﻿using System;
using System.Collections.Generic;
using Com.PhilChuang.Mobile.Apps.LocationSender.Model.Data;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.Pages;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.ViewModels;
using Com.PhilChuang.Utils.Mobile;

namespace Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.ViewFacades
{
	public class SelectFormatViewFacade : AbstractLocationSenderAppViewFacade<SelectFormatPage, SelectFormatPageViewModel>, ISelectFormatView
	{
		#region Events
		public event Action DoReloadData = delegate { };
		public event Action SendMessage = delegate { }; 
		#endregion

		#region Instance Variables (EMPTY)
		#endregion

		#region Properties
		public IList<String> Recipients
		{
			get { return myViewModel.Recipients; }
			set
			{
				if (DoNotAccessViewModel ()) return;
				InvokeSet (val => {
				           	if (DoNotAccessViewModel ()) return;
				           	myViewModel.HasBeenLoaded = true;
				           	myViewModel.Recipients.Clear ();
				           	if (val != null)
				           		myViewModel.Recipients.AddRange (val);
				           },
				           value);
			}
		}
		public GeoCoordinateFormat Format
		{
			get { return myViewModel.Format; }
			set
			{
				if (DoNotAccessViewModel ()) return;
				InvokeSet (val => {
				           	if (DoNotAccessViewModel ()) return;
				           	myViewModel.HasBeenLoaded = true;
				           	myViewModel.Format = val;
				           },
				           value);
			}
		}
		#endregion

		#region Constructors
		public SelectFormatViewFacade ()
		{
			Init (false);
		}

		public SelectFormatViewFacade (SelectFormatPage page)
		{
			myPage = page;
			Init (true);
		}

		private void Init (bool wireUp)
		{
			myViewModel = new SelectFormatPageViewModel ();
			myViewModel.IsAssociatedWithViewFacadeAndPage = true;
			myViewModel.HasBeenLoaded = false;
			if (wireUp) WireUpPage ();
		}
		#endregion

		#region Implementation of IView/ISelectFormatView
		// no methods to implement
		#endregion

		/// <summary>
		/// Unsubscribes view event handlers.
		/// Call base impl as last step.
		/// </summary>
		protected override void UnWirePage ()
		{
			if (myPage == null) return;

			// ISelectFormatView
			myPage.SendMessage -= FireSendMessage;

			base.UnWirePage ();
		}

		/// <summary>
		/// Subscribes view event handlers.
		/// Call base impl as first step.
		/// </summary>
		protected override void WireUpPage ()
		{
			if (myPage == null) return;

			base.WireUpPage ();

			// ISelectFormatView
			myPage.SendMessage += FireSendMessage;
		}

		protected override void FireReload ()
		{ DoReloadData (); }

		protected void FireSendMessage ()
		{ SendMessage (); }
	}
}
